home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / misc / unix / binsplit_c < prev    next >
Encoding:
Text File  |  1996-08-27  |  1.6 KB  |  72 lines

  1. /*
  2. **    NAME        = binsplit
  3. **    VERSION    = 0.2
  4. **    AUTHOR    = dbalster
  5. **    COMMENT    = splits huge files into pieces
  6. **
  7. **    USAGE    (splitting a huge file)
  8. **    ---------------------------------
  9. **    > gcc binsplit.c -o binsplit
  10. **    > binsplit <sourcefile> <blocksize> <destinationfile basename>
  11. **
  12. **    USAGE    (joining the pieces)
  13. **    ------------------------------
  14. **    in an unix shell type:
  15. **    > cat piece.* > <huge file name>
  16. **    in an amigados shell type:
  17. **    > join piece.* TO <huge file name>
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. int main (int argc, char **argv)
  24. {
  25.     FILE *in,*out;
  26.     int size;
  27.     int bytes;
  28.     char buf[256];
  29.     char *ptr;
  30.  
  31.     if (argc != 4)
  32.     {
  33.         printf("Usage: %s <source> <blocksize> <destination>\n"
  34.             "where\n"
  35.             " - blocksize means the number of bytes per block\n"
  36.             " - source is the name of the file to split (read)\n"
  37.             " - destination is the name of the file(s) to create\n"
  38.             "\ndestination files were numbered <name>.0, <name>.1, ...\n\n",argv[0]);
  39.         return 1;    /* error */
  40.     }
  41.     size = atoi(argv[2]);
  42.     in = fopen(argv[1],"r");
  43.     if (in==0) return 2;
  44.     printf("Blocksize is: %ld\n\n",size);
  45.     bytes = 0;
  46.     if (ptr = malloc(size))
  47.     {
  48.         int block=0;
  49.     
  50.         while (!feof(in))
  51.         {
  52.             sprintf(buf,"%s.%ld",argv[3],block++);
  53.             if (out=fopen(buf,"w"))
  54.             {
  55.                 printf("Reading from \"%s\" %ld bytes\n",argv[1],size);
  56.                 bytes = fread(ptr,1,size,in);
  57.                 printf("Writing to \"%s\" %ld bytes\n",buf,bytes);
  58.                 fwrite(ptr,1,bytes,out);
  59.                 fclose(out);
  60.             }
  61.             if (ferror(in)) { printf("input file error!\n");break;}
  62.             if (ferror(out)) { printf("output file error!\n");break;}
  63.         }
  64.     
  65.         free(ptr);
  66.     }
  67.     
  68.     fclose(in);
  69.  
  70.     return 0;
  71. }
  72.